package com.sanjin.controller;

import com.sanjin.annotations.SysLogOperation;
import com.sanjin.entity.Admin;
import com.sanjin.entity.ParkOrder;
import com.sanjin.entity.ParkSpace;
import com.sanjin.entity.WxMiniUser;
import com.sanjin.service.ParkOrderService;
import com.sanjin.service.ParkSpaceService;
import com.sanjin.utils.ResponseVoUtils;
import com.sanjin.utils.SecurityUtils;
import com.sanjin.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @description:
 * @author: sanjin
 * @date: 2019/5/8 11:10
 */
@Api(tags = "订单业务")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private ParkOrderService orderService;

    @Autowired
    private ParkSpaceService parkSpaceService;

    @Autowired
    private SecurityUtils securityUtils;

    @SysLogOperation(value = "【创建订单】")
    @ApiOperation(value = "【创建订单】")
    @PostMapping("/create")
    public ResponseVo createParkOrder(@RequestParam Integer parkSpaceId,
                                  @RequestParam BigDecimal spendTime) {
        Integer wxMiniUserId = securityUtils.getWxMiniUser().getId();

        ParkSpace parkSpace = parkSpaceService.queryParkSpaceByParkSpaceId(parkSpaceId);
        // 计算需要的积分
        BigDecimal amount = parkSpace.getPrice().multiply(spendTime);
        ParkOrder order = new ParkOrder()
                .setSpendTime(spendTime)
                .setWxMiniUserId(wxMiniUserId)
                .setAmount(amount);

        ParkOrder save = orderService.createOrder(order);
        return ResponseVoUtils.success(save);
    }

    @SysLogOperation(value = "【支付订单】")
    @ApiOperation(value = "【支付订单】")
    @PostMapping("/pay")
    public ResponseVo payParkOrder(@RequestParam Integer orderId) {
        Integer wxMiniUserId = securityUtils.getWxMiniUser().getId();
        ParkOrder order = orderService.payOrder(orderId, wxMiniUserId);
        return ResponseVoUtils.success(order);
    }

    @SysLogOperation(value = "【取消订单】")
    @ApiOperation(value = "【取消订单】")
    @PostMapping("/cancel")
    public ResponseVo cancelParkOrder(@RequestParam Integer orderId) {
        Integer wxMiniUserId = securityUtils.getWxMiniUser().getId();
        ParkOrder order = orderService.cancelOrder(orderId, wxMiniUserId);
        return ResponseVoUtils.success(order);
    }

    @ApiOperation(value = "【获取所有订单】")
    @GetMapping("/list")
    public ResponseVo getAllParkOrders() {
        List<ParkOrder> allParkOrders = orderService.getAllOrders();
        return ResponseVoUtils.success(allParkOrders);
    }


}
